package com.autonavi.abs.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.autonavi.abs.common.aop.annotation.IgnoreLogField;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Field;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wwy
 * @version V1.0
 * <p>Description: 日志打印工具类</p>
 * @date 2020-07-20 15:22
 */
public class LogUtils {

    /**
     * 获取被LogIgnore修饰的属性名
     *
     * @param object
     * @return
     */
    public static List<String> getIgnoreFieldNameList(Object object) {

        List<String> result = Lists.newArrayList();
        if (object == null) {
            return result;
        }

        // 获取所有字段
        Field[] fields = object.getClass().getDeclaredFields();
        if (ArrayUtils.isEmpty(fields)) {
            return result;
        }

        // 获取所有被LogIgnore修饰的字段
        List<String> ignoreFieldNameList = Stream.of(fields).filter((field) -> {
            field.setAccessible(true);
            return field.getAnnotation(IgnoreLogField.class) != null;
        }).map((field) -> field.getName()).collect(Collectors.toList());

        return ignoreFieldNameList;

    }

    /**
     * 获取对象可用字段的json
     *
     * @param object
     * @return
     */
    public static String getValidJson(Object object) {

        if (object == null) {
            return "";
        }

        // 获取所有被LogIgnore修饰的字段
        List<String> ignoreFieldNameList = getIgnoreFieldNameList(object);

        // 设置json的忽略字段
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
        if (CollectionUtils.isNotEmpty(ignoreFieldNameList)) {
            filter.getExcludes().addAll(ignoreFieldNameList);
        }

        return JSON.toJSONString(object, filter);

    }

    /**
     * 获取对象列表 可用字段的json
     *
     * @param objectList
     * @return
     */
    public static List<String> getValidListJson(List<Object> objectList) {

        List<String> result = Lists.newArrayList();
        if (CollectionUtils.isEmpty(objectList)) {
            return result;
        }

        objectList.stream().forEach((object) -> {
            String json = getValidJson(object);
            result.add(json);
        });

        return result;

    }

    /**
     * 获取对象数组可用字段的json
     * @param params
     * @return
     */
    public static List<String> getValidListJson(Object[] params){

        List<String> paramJsonList = Lists.newArrayList();
        if(ArrayUtils.isEmpty(params)){
            return paramJsonList;
        }

        Stream.of(params).forEach((param) -> {
            try {
                String json = LogUtils.getValidJson(param);
                paramJsonList.add(json);
            } catch (Exception e) {
                // 部分复杂对象不支持json操作，此处忽略这类对象，如HttpServletRequest
            }
        });

        return paramJsonList;
    }

}
